Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Runtime.InteropServices
Imports System.Configuration
Imports System.Text
Public Class UserPermission
    Public DBConnectstring As String
    Public SESSION_USER_ID As String
    Public SESSION_VIAKLIX_ID As String
    Public sErrMsg As String

    'Public Function GetUserPermissions(ByVal sUID As String, _
    '        ByVal sVID As String, _
    'ByVal nCID As Integer) As Array

    '    Dim Rst As DataSet
    '    Dim Rst1 As DataSet
    '    Dim Rst2 As DataSet
    '    'Dim Rst3 As DataSet
    '    Dim sSQL As String
    '    Dim nUserCnt As Integer
    '    Dim aUsers As Object
    '    Dim i As Integer
    '    Dim bReturn As Boolean
    '    Dim aPermissions As Array
    '    Dim nUCnt As Integer
    '    Dim sErr As String = ""
    '    sSQL = ""

    '    sSQL = sSQL & "select "
    '    sSQL = sSQL & "     USERS.USER_ID, "
    '    sSQL = sSQL & "     USERS.SUPERVISOR "
    '    sSQL = sSQL & "from "
    '    sSQL = sSQL & "     USERS "
    '    sSQL = sSQL & "where "
    '    sSQL = sSQL & "     USERS.VIAKLIX_ID = '" & sVID & "' and "
    '    sSQL = sSQL & "     USERS.COMPANY_ID = " & nCID & " "
    '    Try

    '        Rst = New DataSet
    '        GetDataset(DBConnectstring, sSQL, "Users", sErr, Rst)

    '        If Rst.Tables(0).Rows.Count > 0 Then

    '            nUserCnt = Rst.Tables(0).Rows.Count

    '            ReDim aUsers(nUserCnt, 3)
    '            For i = 0 To nUserCnt - 1 Step 1
    '                aUsers(i, 0) = Rst.Tables(0).Rows(i).Item("USER_ID")   'Rst(0)
    '                aUsers(i, 1) = Rst.Tables(0).Rows(i).Item("SUPERVISOR")                 ''SafeString(Rst(1))
    '                If Rst.Tables(0).Rows(i).Item("USER_ID") = sUID Then
    '                    aUsers(i, 2) = 1
    '                Else
    '                    aUsers(i, 2) = 0
    '                End If

    '            Next i
    '            Rst = Nothing
    '            bReturn = TagUserSubordinates(aUsers, sUID)
    '            nUCnt = 0
    '            For i = 0 To UBound(aUsers) - 1 Step 1
    '                If aUsers(i, 2) = 1 Then
    '                    sSQL = ""
    '                    sSQL = sSQL & "select "
    '                    sSQL = sSQL & "     count(USER_PERMISSIONS.USER_ID) as UCnt "
    '                    sSQL = sSQL & "from "
    '                    sSQL = sSQL & "     USER_PERMISSIONS "
    '                    sSQL = sSQL & "where "
    '                    sSQL = sSQL & "     USER_PERMISSIONS.USER_ID = '" & aUsers(i, 0) & "'"
    '                    Rst1 = New DataSet
    '                    GetDataset(DBConnectstring, sSQL, "Users", sErr, Rst1)

    '                    If Rst1.Tables(0).Rows.Count > 0 Then
    '                        nUCnt = nUCnt + (Rst1.Tables(0).Rows(0).Item("UCnt"))
    '                    End If


    '                End If
    '            Next i
    '            'ReDim aPermissions(nUCnt, 3)
    '            nUCnt = 0
    '            For i = 0 To UBound(aUsers) - 1 Step 1
    '                If aUsers(i, 2) = 1 Then
    '                    sSQL = ""
    '                    sSQL = sSQL & "select "
    '                    sSQL = sSQL & "     USER_PERMISSIONS.USER_ID, "
    '                    sSQL = sSQL & "     USER_PERMISSIONS.PERMISSION_TYPE_ID, "
    '                    sSQL = sSQL & "     USER_PERMISSION_TYPES.PERMISSIONDESCRIPTION "
    '                    sSQL = sSQL & "from "
    '                    sSQL = sSQL & "     USER_PERMISSIONS, "
    '                    sSQL = sSQL & "     USER_PERMISSION_TYPES "
    '                    sSQL = sSQL & "where "
    '                    sSQL = sSQL & "     USER_PERMISSIONS.USER_ID = '" & aUsers(i, 0) & "' and "
    '                    sSQL = sSQL & "     USER_PERMISSIONS.VIAKLIX_ID = '" & sVID & "' and "
    '                    sSQL = sSQL & "     USER_PERMISSIONS.COMPANY_ID = " & nCID & " AND "
    '                    sSQL = sSQL & "     USER_PERMISSIONS.PERMISSION_TYPE_ID = USER_PERMISSION_TYPES.PERMISSION_TYPE_ID "
    '                    Rst2 = New DataSet
    '                    GetDataset(DBConnectstring, sSQL, "Users", sErr, Rst2)

    '                    'If Rst2.Tables(0).Rows.Count > 0 Then
    '                    '    For a = 0 To Rst2.Tables(0).Rows.Count - 1 Step 1
    '                    '        aPermissions(nUCnt, 0) = Rst2.Tables(0).Rows(a).Item("USER_ID")
    '                    '        aPermissions(nUCnt, 1) = Rst2.Tables(0).Rows(a).Item("PERMISSION_TYPE_ID")
    '                    '        aPermissions(nUCnt, 2) = Rst2.Tables(0).Rows(a).Item("PERMISSIONDESCRIPTION")
    '                    '        'Rst.MoveNext()
    '                    '        nUCnt = nUCnt + 1
    '                    '    Next
    '                    'End If
    '                    Rst = Nothing
    '                End If
    '            Next i
    '            'GetUserPermissions = aPermissions


    '        Else
    '            'Rst = Nothing
    '            GetUserPermissions = Nothing
    '        End If
    '    Catch ex As SqlException

    '        i = 0
    '        For i = 0 To ex.Errors.Count - 1
    '            sErr = ex.Errors(i).Message.ToString()
    '        Next i
    '        ''/**use this code to debug**//

    '        LogErrorEvent(sErr)
    '        GetUserPermissions = Nothing
    '    End Try


    'End Function

    'Public Function GetPermissionTypes(ByVal sUID As String, ByVal sVID As String, ByVal CID As String, ByVal sType As String) As Object


    '    'Dim Conn As ADODB.Connection
    '    Dim Rst As DataSet
    '    Dim Rst1 As DataSet
    '    Dim Rst2 As DataSet
    '    Dim Rst3 As DataSet
    '    Dim sSQL As String
    '    Dim sSupervisor As String = ""
    '    Dim aGetTypes() As Array
    '    Dim nCnt As Integer
    '    Dim sErr As String
    '    'Conn = New ADODB.Connection
    '    'Conn.Open(DBConnectstring)
    '    If SESSION_VIAKLIX_ID = "000000" Or SESSION_USER_ID = SESSION_VIAKLIX_ID Then
    '        sSQL = "SELECT * "
    '        sSQL = sSQL & "FROM USER_PERMISSION_TYPES "

    '        If sType = "virtualterminaltype" Then
    '            sSQL = sSQL & "WHERE PERMISSION_TYPE_ID BETWEEN 1 and 7 "
    '        ElseIf sType = "usermngttype" Then
    '            sSQL = sSQL & "WHERE PERMISSION_TYPE_ID BETWEEN 8 and 11 "

    '        ElseIf sType = "terminalsetup" Then
    '            sSQL = sSQL & "WHERE PERMISSION_TYPE_ID = 12 "
    '        End If
    '        'GetDataset(DBConnectstring, "permtype", sSQL, sErr, Rst)

    '        If Rst.Tables(0).Rows.Count > 0 Then

    '            nCnt = 0
    '            nCnt = Rst.Tables(0).Rows.Count

    '            ' ReDim aGetTypes(nCnt, 2)
    '            'Rst.Requery()
    '            nCnt = 0
    '            For nCnt = 0 To Rst.Tables(0).Rows.Count - 1
    '                'aGetTypes(nCnt, 0) = Rst.Tables(0).Rows(nCnt).Item("permission_type_id")
    '                'aGetTypes(nCnt, 1) = Rst.Tables(0).Rows(nCnt).Item("permissiondescription")
    '                'nCnt = nCnt + 1
    '            Next
    '            'Do While Not Rst.EOF
    '            '    aGetTypes(nCnt, 0) = Rst("permission_type_id")
    '            '    aGetTypes(nCnt, 1) = Rst("permissiondescription")
    '            '    nCnt = nCnt + 1
    '            '    Rst.MoveNext()
    '            'Loop
    '            GetPermissionTypes = aGetTypes

    '            Exit Function
    '        Else

    '            GetPermissionTypes = " "

    '            Exit Function
    '        End If
    '    End If
    '    If SESSION_USER_ID <> SESSION_VIAKLIX_ID And SESSION_VIAKLIX_ID <> "000000" Then
    '        sSQL = ""
    '        sSQL = sSQL & "SELECT Supervisor "
    '        sSQL = sSQL & "FROM "
    '        sSQL = sSQL & "         USERS "
    '        sSQL = sSQL & "WHERE "
    '        sSQL = sSQL & "         USER_ID = '" & sUID & "' and "
    '        sSQL = sSQL & "         VIAKLIX_ID = '" & sVID & "' and "
    '        sSQL = sSQL & "         COMPANY_ID = " & CInt(CID) & " "
    '        'Rst = New ADODB.Recordset
    '        'Rst.ActiveConnection = Conn
    '        'Rst = Conn.Execute(sSQL)
    '        GetDataset(DBConnectstring, sSQL, "merchant", sErr, Rst1)
    '        If Rst1.Tables(0).Rows.Count > 0 Then
    '            sSupervisor = Trim(Rst.Tables(0).Rows(0).Item("supervisor"))
    '        End If
    '        If sSupervisor <> "" And sSupervisor <> sVID Then
    '            sSQL = ""
    '            sSQL = sSQL & "select "

    '            sSQL = sSQL & "     USER_PERMISSIONS.PERMISSION_TYPE_ID, "
    '            sSQL = sSQL & "     USER_PERMISSION_TYPES.PERMISSIONDESCRIPTION "
    '            sSQL = sSQL & "from "
    '            sSQL = sSQL & "     USER_PERMISSIONS, "
    '            sSQL = sSQL & "     USER_PERMISSION_TYPES "
    '            sSQL = sSQL & "where "
    '            If SESSION_USER_ID <> sUID Then
    '                sSQL = sSQL & "     USER_PERMISSIONS.USER_ID = '" & SESSION_USER_ID & "' and "
    '            Else
    '                sSQL = sSQL & "     USER_PERMISSIONS.USER_ID = '" & sUID & "' and "
    '            End If
    '            sSQL = sSQL & "     USER_PERMISSIONS.VIAKLIX_ID = '" & sVID & "' and "
    '            sSQL = sSQL & "     USER_PERMISSIONS.COMPANY_ID = " & CInt(CID) & " AND "
    '            If sType = "virtualterminaltype" Then
    '                sSQL = sSQL & "     USER_PERMISSION_TYPES.PERMISSION_TYPE_ID BETWEEN 1 and 7 And "
    '            ElseIf sType = "usermngttype" Then
    '                sSQL = sSQL & "     USER_PERMISSION_TYPES.PERMISSION_TYPE_ID  BETWEEN 8 and 11  And "

    '            ElseIf sType = "terminalsetup" Then
    '                sSQL = sSQL & "     USER_PERMISSION_TYPES.PERMISSION_TYPE_ID = 12 And "
    '            End If
    '            sSQL = sSQL & "     USER_PERMISSIONS.PERMISSION_TYPE_ID = USER_PERMISSION_TYPES.PERMISSION_TYPE_ID "
    '            'Rst = New ADODB.Recordset
    '            'Rst.ActiveConnection = Conn
    '            'Rst.CursorLocation = adUseClient
    '            'Rst.Open(sSQL, Conn, adOpenForwardOnly, adLockReadOnly, adCmdText)
    '            GetDataset(DBConnectstring, sSQL, "merchant", sErr, Rst2)
    '            If Rst2.Tables(0).Rows.Count > 0 Then
    '                nCnt = 0
    '                nCnt = Rst2.Tables(0).Rows.Count
    '                'Do While Not Rst.EOF
    '                '    nCnt = nCnt + 1
    '                '    Rst.MoveNext()
    '                'Loop
    '                'ReDim aGetTypes(nCnt, 2)
    '                'Rst.Requery()
    '                nCnt = 0
    '                For nCnt = 0 To Rst2.Tables(0).Rows.Count - 1
    '                    'aGetTypes(nCnt, 0) = Rst2.Tables(0).Rows(nCnt).Item("permission_type_id")
    '                    'aGetTypes(nCnt, 1) = Rst2.Tables(0).Rows(nCnt).Item("permissiondescription")
    '                Next
    '                'Do While Not Rst.EOF
    '                '    aGetTypes(nCnt, 0) = Rst("permission_type_id")
    '                '    aGetTypes(nCnt, 1) = Rst("permissiondescription")
    '                '    nCnt = nCnt + 1
    '                '    Rst.MoveNext()
    '                'Loop
    '                GetPermissionTypes = aGetTypes
    '                'Rst.Close()
    '                'Rst = Nothing
    '                'Conn.Close()
    '                'Conn = Nothing
    '                Exit Function
    '            Else

    '                GetPermissionTypes = " "
    '                'Rst.Close()
    '                'Rst = Nothing
    '                'Conn.Close()
    '                'Conn = Nothing
    '                Exit Function
    '            End If
    '        Else
    '            If sSupervisor = sVID Then
    '                sSQL = "SELECT "
    '                sSQL = sSQL & "     USER_PERMISSIONS.PERMISSION_TYPE_ID, "
    '                sSQL = sSQL & "     USER_PERMISSION_TYPES.PERMISSIONDESCRIPTION "
    '                sSQL = sSQL & "FROM USER_PERMISSIONS, "
    '                sSQL = sSQL & "     USER_PERMISSION_TYPES "
    '                sSQL = sSQL & "Where"
    '                If SESSION_USER_ID <> sUID Then
    '                    sSQL = sSQL & "     USER_PERMISSIONS.USER_ID = '" & SESSION_USER_ID & "' and "
    '                Else
    '                    sSQL = sSQL & "     USER_PERMISSIONS.USER_ID = '" & sUID & "' and "
    '                End If
    '                sSQL = sSQL & "     USER_PERMISSIONS.VIAKLIX_ID = '" & sVID & "' and "
    '                sSQL = sSQL & "     USER_PERMISSIONS.COMPANY_ID = " & CInt(CID) & " AND "
    '                If sType = "virtualterminaltype" Then
    '                    sSQL = sSQL & "USER_PERMISSION_TYPES.PERMISSION_TYPE_ID BETWEEN 1 and 7 AND "
    '                ElseIf sType = "usermngttype" Then
    '                    sSQL = sSQL & "USER_PERMISSION_TYPES.PERMISSION_TYPE_ID BETWEEN 8 and 11 AND "

    '                ElseIf sType = "terminalsetup" Then
    '                    sSQL = sSQL & "USER_PERMISSION_TYPES.PERMISSION_TYPE_ID = 12 AND "
    '                End If
    '                sSQL = sSQL & "     USER_PERMISSIONS.PERMISSION_TYPE_ID = USER_PERMISSION_TYPES.PERMISSION_TYPE_ID "
    '                'Rst = New ADODB.Recordset
    '                'Rst.ActiveConnection = Conn
    '                'Rst.CursorLocation = adUseClient
    '                'Rst.Open(sSQL, Conn, adOpenForwardOnly, adLockReadOnly, adCmdText)
    '                Rst3 = New DataSet
    '                GetDataset(DBConnectstring, sSQL, "merchant", sErr, Rst3)
    '                If Rst3.Tables(0).Rows.Count > 0 Then
    '                    nCnt = 0
    '                    nCnt = Rst3.Tables(0).Rows.Count
    '                    'Do While Not Rst.EOF
    '                    '    nCnt = nCnt + 1
    '                    '    Rst.MoveNext()
    '                    'Loop
    '                    'ReDim aGetTypes(nCnt, 2)
    '                    'Rst.Requery()
    '                    nCnt = 0
    '                    For nCnt = 0 To Rst3.Tables(0).Rows.Count - 1
    '                        'aGetTypes(nCnt, 0) = Rst3.Tables(0).Rows(nCnt).Item("permission_type_id")
    '                        'aGetTypes(nCnt, 1) = Rst3.Tables(0).Rows(nCnt).Item("permissiondescription")
    '                    Next
    '                    'Do While Not Rst.EOF
    '                    '    aGetTypes(nCnt, 0) = Rst("permission_type_id")
    '                    '    aGetTypes(nCnt, 1) = Rst("permissiondescription")
    '                    '    nCnt = nCnt + 1
    '                    '    Rst.MoveNext()
    '                    'Loop
    '                    GetPermissionTypes = aGetTypes
    '                    'Rst.Close()
    '                    'Rst = Nothing
    '                    'Conn.Close()
    '                    'Conn = Nothing
    '                    Exit Function
    '                Else

    '                    'GetPermissionTypes = " "
    '                    'Rst.Close()
    '                    'Rst = Nothing
    '                    'Conn.Close()
    '                    'Conn = Nothing

    '                End If
    '            End If
    '        End If
    '    End If
    '    Return aGetTypes

    'End Function

    Private Sub LogErrorEvent(ByVal sOrigin As String)
        'Dim sErrMsg As String
        sErrMsg = ""
        sErrMsg = sErrMsg & "viaUserManagment.UserPermission" & sOrigin & vbCrLf
        sErrMsg = sErrMsg & "Source: " & Err.Source & vbCrLf
        sErrMsg = sErrMsg & "Number: " & Err.Number & vbCrLf
        sErrMsg = sErrMsg & "Description: " & Err.Description
        'App.LogEvent(sErrMsg, vbLogEventTypeError)
        'Debug.Print(sErrMsg)
    End Sub

    Public Function UpdateUserpermissions(ByVal sUID As String, ByVal CID As String, ByVal sVID As String, ByVal addrights As String, ByVal delrights As String) As Boolean
        'On Error GoTo ErrorHandler
        Dim Cmd As SqlCommand = Nothing
        Dim Conn As New SqlConnection
        'Dim Rs As DataSet
        Dim sErr As String = ""

        If Len(delrights) > 0 Then
            Cmd = New SqlCommand
            With Cmd

                .Connection = Conn
                .CommandType = CommandType.StoredProcedure
                .CommandText = ".DelUserPermission"
                .Parameters.Add(New SqlParameter("@i_cid", SqlDbType.Int, 15)).Value = CInt(CID)
                .Parameters.Add(New SqlParameter("@i_vid", SqlDbType.VarChar, 15)).Value = sVID
                .Parameters.Add(New SqlParameter("@i_userid", SqlDbType.VarChar, 15)).Value = sUID
                .Parameters.Add(New SqlParameter("@i_permissiontypeid", SqlDbType.VarChar, 255)).Value = delrights
                .Parameters.Add(New SqlParameter("@o_errormsg", SqlDbType.VarChar, 255)).Direction = ParameterDirection.Output
            End With
            Try
                Cmd.Connection.Open()
                Cmd.ExecuteNonQuery()

                '------------------------------------------------------------------------
                'Returns a message to the calling ASP page.
                '------------------------------------------------------------------------
                If Not IsNothing(Cmd.Parameters("o_errormsg").Value) Then
                    sErr = Cmd.Parameters("o_errormsg").Value

                End If
                '------------------------------------------------------------------------
            Catch ex As SqlException
                'Dim strErrEx As String = ""
                'Dim strTDErr As String
                Dim i As Integer = 0

                For i = 0 To ex.Errors.Count - 1
                    sErr = ex.Errors(i).Message.ToString()
                Next i
                ''/**use this code to debug**//

                LogErrorEvent(sErr)

            Finally
                If Not (Conn Is Nothing) Then
                    Conn.Close()
                End If
            End Try

        End If

        If Len(addrights) > 0 Then
            With Cmd
                .Connection = Conn
                .CommandType = CommandType.StoredProcedure
                .CommandText = "InsUserPermission"
                .Parameters.Add(New SqlParameter("@i_cid", SqlDbType.Int, 15)).Value = CInt(CID)
                .Parameters.Add(New SqlParameter("@i_vid", SqlDbType.VarChar, 15)).Value = sVID
                .Parameters.Add(New SqlParameter("@i_userid", SqlDbType.VarChar, 15)).Value = sUID
                .Parameters.Add(New SqlParameter("@i_permissiontypeid", SqlDbType.VarChar, 255)).Value = addrights
                .Parameters.Add(New SqlParameter("@o_errormsg", SqlDbType.VarChar, 255)).Direction = ParameterDirection.Output
            End With
            Try
                Cmd.Connection.Open()
                Cmd.ExecuteNonQuery()
                '------------------------------------------------------------------------
                'Returns a message to the calling ASP page.
                '------------------------------------------------------------------------
                If Not IsNothing(Cmd.Parameters("o_errormsg").Value) Then

                    sErr = Cmd.Parameters("o_errormsg").Value

                End If
                '------------------------------------------------------------------------
            Catch ex As SqlException
                'Dim strErrEx As String = ""
                'Dim strTDErr As String
                Dim i As Integer = 0

                For i = 0 To ex.Errors.Count - 1
                    sErr = ex.Errors(i).Message.ToString()
                Next i
                ''/**use this code to debug**//

                LogErrorEvent(sErr)

            Finally
                If Not (Conn Is Nothing) Then
                    Conn.Close()
                End If
            End Try
        End If
        UpdateUserpermissions = True
        Exit Function
        'ErrorHandler:
        'Call LogErrorEvent(".UpdateUserpermissions")
        Exit Function
    End Function

    Private Function TagUserSubordinates(ByRef aUsers As Object, _
            ByVal sUID As String) As Boolean
        '-----  Function: UserManagement.TagUserSubordinates
        '       Author: Brian S. Ward, for Nova Information Systems
        '       Date: 09.10.2001
        '       Purpose:    This function will recursively call itself
        '                   following parent-child relationships within
        '                   aUsers, tagging all indexes that are a child
        '                   or ancestor of the original passed-in sUID.
        '                   The tagging is handled by the 3rd dimension of
        '                   the aUsers array, 1 if tagged, 0 if not.

        Dim i As Integer
        Dim nReturn As Boolean
        Dim sPassUID As String
        For i = 0 To UBound(aUsers) - 1 Step 1
            If UCase(CStr(aUsers(i, 1))) = UCase(sUID) Then
                aUsers(i, 2) = 1
                sPassUID = aUsers(i, 0)
                nReturn = TagUserSubordinates(aUsers, sPassUID)
            End If
        Next i
        nReturn = True

        'ErrorHandler:
        '        Call LogErrorEvent(".TagUserSubordinates")
        '        Exit Function
        Return nReturn
    End Function

End Class
